package com.qf.xiaomi.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

/*** @author liyao
 * @version V1.0
 * @Project Second
 * @Package com.qf.utils
 * @Description: ${TODO} 
 * @Date 2022/8/2 17:31 
 */
public class DButils {
    private static DruidDataSource dataSource;

   static Properties properties = new Properties();
   static {
       InputStream is = DButils.class.getResourceAsStream("/user.properties");
       try {
           properties.load(is);
          dataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
   public static DruidDataSource getdataSource(){
       return dataSource;
   }
   public static void begin(){
       try {
           DruidPooledConnection connection = dataSource.getConnection();
           connection.setAutoCommit(false);
       } catch (SQLException e) {
           e.printStackTrace();
       }
   }
   public static void commit(){
       try {
           DruidPooledConnection connection = dataSource.getConnection();
           connection.commit();
           connection.setAutoCommit(true);
       } catch (SQLException e) {
           e.printStackTrace();
       }
   }
   public static void rollback(){
       try {
           DruidPooledConnection connection = dataSource.getConnection();
           connection.rollback();
           connection.setAutoCommit(true);
       } catch (SQLException e) {
           e.printStackTrace();
       }
   }
}
